var Users = (function Users() {

    var UserRoles = {
        ADMINISTRATOR: 1,
        STANDARDOWY_UZYTKOWNIK: 2
    };

    var isValidUserData = function (data) {
        var result = {
            success: false,
            error: ''
        };
        if ($.isEmptyObject(data.username)) {
            result.error = 'Pole nazwa użytkownika jest puste.';
        } else if ($.isEmptyObject(data.password)) {
            result.error = 'Pole hasło jest puste.';
        } else if ($.isEmptyObject(data.password2)) {
            result.error = 'Pole powtórz hasło jest puste.';
        } else if ($.isEmptyObject(data.role)) {
            result.error = 'Pole rola jest puste.';
        } else {
            result.success = true;
        }
        return result;
    }

    return {
        deleteUser: function (userId) {
            App.showConfirmWindow({
                title: 'Informacja',
                width: 400,
                height: 120,
                body: 'Czy na pewno chcesz usunąć wybranego użytkownika ?',
                onConfirm: function () {
                    App.executeServerAction({
                        url: '/users/delete',
                        type: 'POST',
                        params: {id: userId},
                        onSuccessFn: function () {
                            window.location = 'users';
                        }
                    });
                }
            });
        },
        createTable: function (data, loggedUserId, isAdmin) {
            var users = $.parseJSON(data);
            var table = '<table class="table table-bordered">';
            table += '<thead><tr><th>Login</th><th>Rola</th><th>Aktywny</th><th style="text-align:center">Operacje</th></tr></thead>';
            table += '<tbody>';
            var i = 0;
            for (i; i < users.length; i++) {
                var user = users[i].User;
                var role = users[i].UserRole;
                table += "<tr>";
                table += "<td>" + user.username + "</td>";
                table += "<td>" + role.role + "</td>";
                table += "<td>";
                
                if (user.id != loggedUserId && isAdmin === 1) {
	                if (user.activated == true) {
	                	table += "<img src='img/accept.png' style='cursor:pointer' onclick='Users.setInActive(" + user.id + ")' alt='Deaktywuj'/>";
	                } else {
	                	table += "<img src='img/cancel.png' style='cursor:pointer' onclick='Users.setActive(" + user.id + ")' alt='Aktywuj'/>";
	                }
            	}
                table += "</td>";
                
                var removeImg = "";
                var editImg = "";
                
                if (isAdmin === 1 || user.id == loggedUserId) {
	                if (user.id != loggedUserId) { 
	                	removeImg = " / <img src='/img/user_delete.png' style='cursor:pointer' onclick='Users.deleteUser(" + user.id + ")' />";
	                }
	                editImg = "<img src='/img/user_edit.png' style='cursor:pointer' onclick='Users.displayEditWindow(true," + user.id + ")' />";
            	}

                table += "<td align='center'> " + editImg + removeImg + "</td>";
                table += "</tr>";
            }
            table += '</tbody></table>';
            $('#usersTable').append(table);
        },
        displayEditWindow: function (isEdit, userId) {
            App.createAddEditWindow({
                url: (isEdit == true ? 'users/edit' : 'users/add'),
                title: (isEdit == true ? 'Edytuj użytkownika' : 'Dodaj nowego użytkownika'),
                width: 400,
                height: 300,
                params: (isEdit == true ? {userId: userId } : {}),
                onSave: function () {
                    var data = {
                        username: $('#username').val(),
                        password: $('#password').val(),
                        password2: $('#password2').val(),
                        role: $("#role").val(),
                        id: (isEdit == true ? $('#id').val() : 0)
                    };
                    var valid = isValidUserData(data);
                    if (valid.success == false) {
                        App.showMessage('Błąd', valid.error);
                    } else {
                        App.executeServerAction({
                            url: (isEdit == true ? 'users/edit' : 'users/add'),
                            type: 'POST',
                            params: data,
                            onSuccessFn: function () {
                                window.location = "users";
                            }
                        });
                    }
                }
            });
        },
        setActive: function(userId) {
            App.executeServerAction({
                url: '/users/activate',
                type: 'POST',
                params: {id: userId},
                onSuccessFn: function (response) {
                    window.location = 'users';
                }
            });
        },
        setInActive: function(userId) {
            App.executeServerAction({
                url: '/users/deactivate',
                type: 'POST',
                params: {id: userId},
                onSuccessFn: function () {
                    window.location = 'users';
                }
            });
        },
        getUserRoles: function() {
        	return UserRoles;
        }
    };
})();
